package com.shunbo.convenient.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shunbo.convenient.service.AreaService;
import com.shunbo.convenient.service.StoreService;
import com.shunbo.convenient.service.UserInfoService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.utils.QRCodeUtil;
import com.shunbo.convenient.vo.Area;
import com.shunbo.convenient.vo.Store;
import com.shunbo.convenient.vo.UserInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author weizhuang
 *  店铺
 */
@RestController
@RequestMapping("/store")
public class StoreController {


    @Resource
    private StoreService storeService;
    @Resource
    private AreaService areaService;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 店铺列表
     * @param store 店铺
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getStoreList(@RequestBody Store store){
        try {
            PageHelper.startPage(store.getPageNo(), 10);
            List<Store> all = storeService.findAll(store);
            PageInfo<Store> pageInfo = new PageInfo<>(all);
            Map<String, Object> map = new HashMap<>();
            map.put("areaList", all);
            map.put("pageNo", store.getPageNo());
            map.put("count", pageInfo.getTotal());
            return ConvenientResult.build(200,"成功",map);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }
    /**
     * 添加店铺
     * @param store 店铺
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveStore(@RequestBody Store store, HttpServletRequest request){
        try {
            String encode = null;
            String userId = UUID.randomUUID().toString().replaceAll("-","");
            String realPath ="/home/image";
             encode = QRCodeUtil.encode(
                                    "http://47.104.16.212:8080/userInfo/update?id="
                                    , realPath, true);
             store.setQrCode(encode);
             store.setUser_id(userId);
            Integer save = storeService.save(store);

            UserInfo userInfo = new UserInfo();
            userInfo.setMobile(store.getMobile());
            userInfo.setUserName(store.getName());
            userInfo.setUserId(userId);
            userInfo.setCode("0");
            userInfo.setIcon(store.getImg());
            userInfoService.save(userInfo);
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }
    /**
     * 更新店铺
     * @param store 店铺
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateStore(@RequestBody Store store){
        try {
            Integer save = storeService.update(store);
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }
    /**
     * 删除店铺
     * @param store 店铺
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteStore(@RequestBody Store store){
        try {
            Integer save = storeService.delete(store.getId()+"");
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }
    /**
     * id查店铺
     * @param store 店铺
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getStoreById(@RequestBody Store store){
        try {
            Store st = storeService.findById(store.getId() + "");
            Area byId = areaService.findById(st.getAreaId());
            st.setAreaName(byId.getName());
            return ConvenientResult.build(200,"成功",st);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }
}
